<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入招聘人数" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item label="语言" prop="lang">
        <el-select v-model="queryParams.lang" placeholder="请选择语言">
          <el-option v-for="dict in dict.type.sys_lang" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['fund:jobs:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['fund:jobs:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['fund:jobs:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['fund:jobs:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jobsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="招聘职位" align="center" prop="title" />
      <el-table-column label="工作地点" align="center" prop="location">
        <template slot-scope="scope">
          <span>{{ scope.row.location }}</span>
        </template>
      </el-table-column>
      <el-table-column label="语言" align="center" prop="lang">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_lang" :value="scope.row.lang" />
        </template>
      </el-table-column>

      <el-table-column label="招聘人数" align="center" prop="vacancies" />
      <el-table-column label="学历要求" align="center" prop="education" />
      <el-table-column label="工作经验要求" align="center" prop="workExperience" />
      <el-table-column label="联系邮箱" align="center" prop="contactEmail" />
      <el-table-column label="排序" align="center" prop="sort" width="100">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.sort" type="number" @blur="handleUpdateSort(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="属性" align="center" prop="status">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleSetStatus(scope.row, 'status')"
            :type="scope.row.status == 1 ? '' : 'danger'">{{ scope.row.status == 1 ? "锁定" : "锁定" }}</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="招聘类别" align="center" prop="jobType" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['fund:jobs:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['fund:jobs:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改职位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="语言" prop="lang">
          <el-select v-model="form.lang" placeholder="请选择语言">
            <el-option v-for="dict in dict.type.sys_lang" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="招聘职位" prop="title">
          <el-input v-model="form.title" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="副标题" prop="subTitle">
          <el-input v-model="form.subTitle" placeholder="请输入内容" />
        </el-form-item> -->
        <el-form-item label="锁定" prop="status">
          <el-radio v-model="form.status" :label="1">锁定</el-radio>
          <el-radio v-model="form.status" :label="0">不锁定</el-radio>
        </el-form-item>

        <!-- <el-form-item label="工作地点" prop="location">
          <el-select v-model="form.location" placeholder="请选择" style="width: 300px;">
            <el-option v-for="item in dict.type.job_address" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="职位类别" prop="jobCategory">
          <el-select v-model="form.jobCategory" placeholder="请选择" style="width: 300px;">
            <el-option v-for="item in dict.type.position_type" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="工作地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入工作地点" />
        </el-form-item>
        <el-form-item label="招聘人数" prop="vacancies">
          <el-input v-model="form.vacancies" placeholder="请输入招聘人数" />
        </el-form-item>
        <el-form-item label="学历要求" prop="education">
          <el-input v-model="form.education" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工作经验要求" prop="workExperience">
          <el-input v-model="form.workExperience" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发布日期" prop="publishDate">
          <el-date-picker clearable v-model="form.publishDate" type="date" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择发布日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="岗位职责" prop="responsibilities">
          <editor v-model="form.responsibilities" />
        </el-form-item>
        <el-form-item label="任职资格" prop="qualifications">
          <editor v-model="form.qualifications" />
        </el-form-item>
        <el-form-item label="福利待遇" prop="benefits">
          <editor v-model="form.benefits" />
        </el-form-item>
        <!-- <el-form-item label="联系邮箱" prop="contactEmail">
          <el-input v-model="form.contactEmail" placeholder="请输入联系邮箱" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJobs, getJobs, delJobs, addJobs, updateJobs } from "@/api/fund/jobs";

export default {
  name: "Jobs",
  dicts: ['job_address', 'sys_lang', 'position_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 职位表格数据
      jobsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lang: null,
        title: null,
        subTitle: null,
        location: null,
        jobCategory: null,
        vacancies: null,
        education: null,
        workExperience: null,
        responsibilities: null,
        qualifications: null,
        benefits: null,
        contactEmail: null,
        jobType: this.$route.query.jobType,
        ctime: null,
        mtime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lang: [
          { required: true, message: "请选择语言", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" },
          { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询职位列表 */
    getList() {
      this.loading = true;
      listJobs(this.queryParams).then(response => {
        this.jobsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        lang: null,
        subTitle: null,
        location: null,
        jobCategory: null,
        vacancies: null,
        education: null,
        workExperience: null,
        responsibilities: null,
        qualifications: null,
        benefits: null,
        contactEmail: null,
        jobType: null,
        ctime: null,
        mtime: null,
        publishDate: null,
        status: 0,
        jobType: this.$route.query.jobType
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加职位";
    },
    /** 修改数据状态*/
    handleSetStatus(row, type) {
      const params = {
        id: row.id,
      }
      if (type === 'status') {
        params.status = row.status == 1 ? 0 : 1;
      }
      updateJobs(params).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 修改排序 */
    handleUpdateSort(row) {
      const params = {
        id: row.id,
      }
      params.sort = row.sort;
      updateJobs(params).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getJobs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改职位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateJobs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJobs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除职位编号为"' + ids + '"的数据项？').then(function () {
        return delJobs(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fund/jobs/export', {
        ...this.queryParams
      }, `jobs_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
